package code.ws_common_scene.command.cmd
{
	import code.ws_common_ems.ModeStatusEms;
	import code.ws_common_scene.GameScene;
	import code.ws_common_scene.command.Command;
	import code.ws_common_scene.data.AtkData;
	import code.ws_common_scene.data.HpDamage;
	import code.ws_common_scene.manager.SceneManager;
	import code.ws_common_scene.mode.Hero;
	import code.ws_common_scene.mode.base.Fighter;
	import code.ws_common_scene.mode.inters.ISprite;
	import code.ws_common_utils.IDUtils;
	import code.ws_common_utils.MathUtils;
	
	import flash.geom.Point;

	/**
	 * 攻击指令 
	 * @author qiangchen
	 * 
	 */	
	public class AttackCommand extends Command
	{
		/** 攻击距离 */
		private var _attackDist:int = 0;
		
		/**
		 * 构造函数 
		 * @param attackDist 攻击距离
		 * 
		 */		
		public function AttackCommand( attackDist:int = 2 )
		{
			super( null, false );
			_attackDist = ( attackDist < 1 ) ? 1 : attackDist;
		}
		
		
		/**
		 * 执行指令 
		 * 
		 */		
		override public function execute():void
		{
			var crtId:int = SceneManager.instance.crtAttackid;
			if( crtId == IDUtils.INVALID_ID )
			{
				crtId = NaN;
				cover();
				return;
			}
			
			var scene:GameScene = SceneManager.instance.scene;
			var target:ISprite = scene._eyeDic[crtId] as ISprite;
			if( target == null )
			{
				crtId = NaN;
				scene = null;
				cover();
				return;
			}
			
			if( ( target as Fighter ).status == ModeStatusEms.DEAD )
			{
				crtId = NaN;
				SceneManager.instance.crtAttackid = IDUtils.INVALID_ID;
				scene = null;
				cover();
				return;
			}
			
			// 先移动到攻击目标位置
			var hero:Hero = scene.hero;
			var dist:int = Point.distance( hero.tiles, target.tiles );
			if( dist <= _attackDist )
			{
//				trace("【ws_mobile】---> The hero attack target!");
				// 确定攻击方向
				var dy:Number = hero.y - (target as Fighter).y;
				var dx:Number = hero.x - (target as Fighter).x;
				hero.setDirection( MathUtils.getRealDirByPixel( Math.atan2( dy, dx ) * 180 / Math.PI ) );
				
				// 设置攻击数据，并调用攻击方法
				var hpdamage:HpDamage = new HpDamage();
				hpdamage._count = hero.power * 0.5 + int( hero.power * Math.random() );
				hpdamage.isCrit = ( Math.random() > 0.6 ) ? true : false;
				var atk:AtkData = new AtkData();
				atk._fromid = hero.showid;
				atk._toid = target.showid;
				atk._damage = hpdamage;
				hero.attack( atk );
				
				// 释放引用对象
				atk = null;
				crtId = NaN;
				scene = null;
				target = null;
				hero = null;
				dist = NaN;
				hpdamage = null;
				dx = dy = NaN;
				return;
			}
			
			var path:Array = scene.findPath( hero.tiles, target.tiles );
			if( path == null || path.length == 0 )
			{
				crtId = NaN;
				scene = null;
				target = null;
				hero = null;
				path = null;
				dist = NaN;
				cover();
				return;
			}
			
			hero.walk( path.slice( 0, path.length-_attackDist ) );
			
			// 释放
			crtId = NaN;
			scene = null;
			target = null;
			hero = null;
			path.length = 0;
			path = null;
			dist = NaN;
		}
		
				
		private function cover():void
		{
			if( _innerCallFn != null )
			{
				_innerCallFn.call( null );
			}
			this.dispose();
		}
		
		
		/**
		 * 指令结束 
		 * 
		 */		
		override public function over():void
		{
			// 进行攻击
//			trace("【ws_mobile】---> The hero had arrived and then attack target!");
			var hero:Hero = SceneManager.instance.scene.hero;
			
			// 攻击数据
			var hpdamage:HpDamage = new HpDamage();
			hpdamage._count = hero.power * 0.5 + int( hero.power * 0.5 * Math.random() );
			hpdamage.isCrit = ( Math.random() > 0.6 ) ? true : false;
			var atk:AtkData = new AtkData();
			atk._fromid = hero.showid;
			atk._toid = SceneManager.instance.crtAttackid;
			atk._damage = hpdamage;
			
			// 确定攻击方向
			var fighter:Fighter = SceneManager.instance.scene._eyeDic[SceneManager.instance.crtAttackid] as Fighter;
			var dx:Number = hero.x - fighter.x;
			var dy:Number = hero.y - fighter.y;
			hero.setDirection( MathUtils.getRealDirByPixel( Math.atan2( dy, dx ) * 180 / Math.PI ) );
			
			// 攻击
			hero.attack( atk );
			
			//释放
			hero = null;
			atk = null;
			hpdamage = null;
			fighter = null;
			dx = dy = NaN;
			
			super.over();
		}
		
		
		/**
		 * 释放 
		 * 
		 */		
		override public function dispose():void
		{
			_attackDist = NaN;
			super.dispose();
		}
		
	}
}